package com.wsc.wisdomchina.activitity.InfoActivity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseFragmentActivity;
import com.wsc.wisdomchina.application.MyApplication;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.constant.Constant;
import com.wsc.wisdomchina.fragment.SelectedIndustryFragment;
import com.wsc.wisdomchina.javaBean.LookAllListBean;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.javaBean.StreetInfoListBean;
import com.wsc.wisdomchina.presenter.ContributorPresenter;
import com.wsc.wisdomchina.view.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.List;

/**
 * author: xw
 * created on: 2017/10/31 13:55
 * description: 资讯
 */
public class MoreInfoActivity extends BaseFragmentActivity implements ContributorView {

    private ViewPager viewPager;
    private PagerSlidingTabStrip tabs;// 新闻栏滑动条
    private Context context;
    private ContributorPresenter contributorPresenter;
    private final String LOOKALLLIST_FLAG="LOOKALLLIST_FLAG";
    private String cidNumber;
    private MyApplication myApplication;
    private final String LOCATION_FLAG="LOCATION_FLAG";
    private List<String> nameList;
    private List<String> idList;
    private ImageView regist_back;
    private TextView tv_headtitle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_info);
        context = this;
        contributorPresenter = new ContributorPresenter(this, context);
        myApplication = (MyApplication)getApplication();
        nameList=myApplication.getModuleName();
        idList=myApplication.getModuleId();
        String locationString = Tools.getSharedPreferencesStrings(Constant.SharePreferenceLocation, "", context);
        if (locationString.length() > 0) {
            StreetInfoListBean streetInfoListBean = JsonTools.jsonStringToObject(locationString, StreetInfoListBean.class);
            cidNumber=streetInfoListBean.getId();
        }
        initView();
        getData();
    }

    private void initView() {
        regist_back=(ImageView) findViewById(R.id.regist_back);
        regist_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("资讯");
        viewPager = (ViewPager)findViewById(R.id.viewPager);
        tabs = (PagerSlidingTabStrip)findViewById(R.id.tabs);
        tabs.setIndicatorHeight(Tools.dip2px(context, 3));
        tabs.setTextSize(Tools.dip2px(context, 14));
        tabs.setTextColor(getResources().getColor(R.color.text_color_one));
        tabs.setSelectedTextColor(getResources().getColor(R.color.little_white));

        if(nameList!=null && idList!=null && nameList.size()>0 && idList.size()>0)
        {
            showTabsViewPager();
        }

    }

    private void  showTabsViewPager()
    {

        try {
            List<Fragment> lastlistfragmentView = new ArrayList<>();
            List<String> lastlistSelectedFragmentinfo = new ArrayList<>();
            for(String id:idList)
            {
                Bundle bundle = new Bundle();
                bundle.putString("id", id);
                SelectedIndustryFragment selectedOneFragment = SelectedIndustryFragment.newInstance(bundle);
                lastlistfragmentView.add(selectedOneFragment);
                lastlistSelectedFragmentinfo.add(SelectedIndustryFragment.class.getSimpleName() + id);
            }
            myApplication.setInformationFragmentSelected(lastlistSelectedFragmentinfo.get(0));
            viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), lastlistfragmentView, nameList));// 设置viewpageradapter
            tabs.setViewPager(viewPager, lastlistfragmentView, lastlistSelectedFragmentinfo, this);
        }catch (Exception e)
        {

        }
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if(flag.equals(LOOKALLLIST_FLAG))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("200")) {
                    String message = JsonTools.objectToJsonString(respObj.getMessage());
                    List<LookAllListBean> listData = JsonTools.jsonStringToList(message, LookAllListBean.class);
                    if (listData != null && listData.size() > 0) {
                        nameList=new ArrayList<>();
                        idList=new ArrayList<>();
                        for(LookAllListBean lookAllListBean:listData)
                        {
                            nameList.add(lookAllListBean.getName());
                            idList.add(lookAllListBean.getId());
                        }
                        if(nameList.size()>0 &&idList.size()>0)
                        {
                            myApplication.setModuleId(idList);
                            myApplication.setModuleName(nameList);
                            showTabsViewPager();
                        }
                    }
                }

            }else if(flag.equals(LOCATION_FLAG))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("200"))
                {
                    String message=JsonTools.objectToJsonString(respObj.getMessage());
                    List<StreetInfoListBean> listData=JsonTools.jsonStringToList(message,StreetInfoListBean.class);
                    if(listData!=null &&listData.size()>0)
                    {
                        Tools.setSharedPreferencesStrings(Constant.SharePreferenceLocation,JsonTools.objectToJsonString(listData.get(0)),context);
                        cidNumber=listData.get(0).getId();
                        //获取新闻消息
                        contributorPresenter.getLookAllList(cidNumber,LOOKALLLIST_FLAG);
                    }
                }
            }

        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {

    }

    private void getData()
    {
        if(nameList==null && idList==null)
        {
            if(cidNumber!=null)
            {
                //获取新闻消息
                contributorPresenter.getLookAllList(cidNumber,LOOKALLLIST_FLAG);
            }else
            {
                //先获取词典Number 再获取新闻消息
                String city= myApplication.getLocationCity();
                String country=myApplication.getLocationDistrict();
                contributorPresenter.getHomeFetchStreet(city,country,LOCATION_FLAG);
            }
        }
    }

//    @Override
//    public void onResume() {
//        super.onResume();
//        getData();
//    }
//
//    @Override
//    public void onPause() {
//        super.onPause();
//        MobclickAgent.onPageEnd(InfoFragment.class.getSimpleName());
//    }

    class ViewPagerAdapter extends FragmentStatePagerAdapter {

        private List<Fragment> listfragmentView;// viewList 集合
        private List<String> TITLES;

        public ViewPagerAdapter(FragmentManager fm, List<Fragment> listfragmentView, List<String> TITLES) {
            super(fm);
            this.listfragmentView = listfragmentView;
            this.TITLES = TITLES;
            // TODO Auto-generated constructor stub
        }

        @Override
        public CharSequence getPageTitle(int position) {
            // TODO Auto-generated method stub
            return TITLES.get(position);
        }

        @Override
        public Fragment getItem(int position) {
            // TODO Auto-generated method stub
            return listfragmentView.get(position);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return TITLES.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub

            Fragment ff = (Fragment) super.instantiateItem(container, position);
            ff.setTargetFragment(listfragmentView.get(position), position);

            return ff;
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            // TODO Auto-generated method stub
            super.destroyItem(container, position, object);
        }

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }



}
